﻿using System;
using System.Collections.Generic;
using System.Linq;
using MiniWeb.Core.Dtos;
using MiniWeb.Core.Entities;
using MiniWeb.Core.Entities;
using WFramework.MvcPager;
using WFramework.ORM;

namespace MiniWeb.Dao
{
    public class SeeProductHistoryDao : BaseDao<SeeProductHistory>
    {
        public PagedList<SeeProductHistory> GetPagedList(int pageIndex, int pageSize)
        {
            var sql = Sql.Builder.Where("1=1");
            sql.OrderBy("CreateDate DESC");
            return PagedList<SeeProductHistory>(pageIndex, pageSize, sql);
        }

        public SeeProductHistory GetById(int id)
        {
            var sql = Sql.Builder.Where("id = @0", id);
            return FirstOrDefault(sql);
        }

        public void Delete(int id)
        {
            var sql = Sql.Builder.Where("ID = @0", id);
            Delete(sql);
        }

        public bool ExistsInData(string account,int pid)
        {
            DateTime nowtime = DateTime.Now;
            DateTime endtiem = nowtime.AddDays(-5);
            var sql = Sql.Builder.Where("createdate BETWEEN @0 AND @1 AND account=@2 AND ProductID=@3 ", nowtime,
                                        endtiem, account, pid);
            return FirstOrDefault<SeeProductHistory>(sql) != null;
        }
        public bool Exists(int id)
        {
            var sql = Sql.Builder.Where("ID = @0", id);
            if (FirstOrDefault(sql) != null)
            {
                return true;
            }
            return false;
        }

        public List<SeeProductHistory> GetHistory(string account)
        {
            var sql = Sql.Builder.Select(
                "DISTINCT top 4 a.ProductID,a.ProductName,a.ProductImg,a.Account,a.Price,a.CreateDate ")
                .From("SeeProductHistory a with(nolock)")
                .Where("account=@0 ", account);
            sql.OrderBy("CreateDate desc");

            return Query<SeeProductHistory>(sql).ToList();
        }

        public List<SeeProductHistory> GetAll(int? top, string strwhere, string orderby)
        {
            string sqltop = "";
            if (top != null)
            {
                sqltop = "TOP " + top.ToString() + " * ";
            }
            else
            {
                sqltop = " * ";
            }
            var sql = Sql.Builder.Select(sqltop).From(" SeeProductHistory WITH(NOLOCK)");
            if (!string.IsNullOrEmpty(strwhere))
            {
                sql.Where(strwhere);
            }
            if (!string.IsNullOrEmpty(orderby))
            {
                sql.OrderBy(orderby);
            }
            else
            {
                sql.OrderBy("CreateDate Desc");
            }

            return Query<SeeProductHistory>(sql).ToList();
        }



    }
}